<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;	
use think\Session;

class Cooperation extends cmsBase {
	//yq
	public function index(){
        if(empty($_GET['name'])){
            $data = Db::name('cms_cooperation')
                ->where(['type'=>1])
                ->where('status','not in',2)
                ->order('id desc')
                ->paginate(20);
        }else{
            $data = Db::name('cms_cooperation')
                ->where('type',1)
                ->where('status','not in',2)
                ->where('title','like',"%{$_GET['name']}%")
                ->order('id','desc')
                ->paginate(10,false,['query'=>$_GET]);
            $count = Db::name('cms_cooperation')
                ->where(['type'=>1])
                ->where('status','not in',2)
                ->where('title','like',"%{$_GET['name']}%")
                ->count();
        }

		$count = Db::name('cms_cooperation')
		->where(['type'=>1])
		->where('status','not in',2)
		->count();
		$page = $data->render();
		return view('cooperation/index',['count'=>$count,'data'=>$data,'page'=>$page]);
	}

	public function indexs(){
	    if(empty($_GET['name'])){
            $data = Db::name('cms_cooperation')
                ->where(['type'=>2])
                ->where('status','not in',2)
                ->order('id desc')
                ->paginate(20);
            $count = Db::name('cms_cooperation')
                ->where(['type'=>2])
                ->where('status','not in',2)
                ->count();
        }else{
	        $data = Db::name('cms_cooperation')->where('type',2)
                ->where('status','not in',2)
                ->where('title','like',"%{$_GET['name']}%")
                ->order('id','desc')
                ->paginate(10,false,['query'=>$_GET]);
            $count = Db::name('cms_cooperation')
                ->where(['type'=>2])
                ->where('status','not in',2)
                ->where('title','like',"%{$_GET['name']}%")
                ->count();
        }

		$this->assign('count',$count);
		$this->assign('data',$data);
		$page = $data->render();
		$this->assign('page',$page);
		return $this->fetch('cooperation/indexs');
	}


	//
	public function hzhb(){
		return $this->fetch('cooperation/add');
	}
	
	public function yqlj(){
		return $this->fetch('cooperation/addyqlj');
	}

	// tijiao hz
	public function add(){
		if(Db::name('cms_cooperation')->where(['title'=>$_POST['title'],'statue'=>1])->find()){
			$this->error('名称已经存在','cooperation/indexs');
		}
		$file = $this->request->file('img');
		$data = [];
		$data = [
			'title'=>$_POST['title'],
			'url' =>$_POST['url'],
			'createtime'=>date('Y-m-d H:i:s'),
			'statue'=>$_POST['statue'],
			'type' => 2,
		];
		if($file){
			$info = $file->validate(['size'=>3300*1000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload/Cooperation');
	            if($info){
	            //成功上传后 获取上传信息
	            //输出 jpg
	                $info->getExtension();
	            //输出 42a79759f284b767dfcb2a0197904287.jpg
	                $img=$info->getFilename();
	                $data['photo'] = date('Ymd').'/'.$img;
	         	}
		}else{
			$this->error('图片不能为空或图片太大','cooperation/indexs');
		}	


		if(Db::name('cms_cooperation')->insert($data)){
			$this->success('添加成功！','cooperation/indexs');
		}else{
			$this->error('添加失败','cooperation/indexs');
		}
	}

	//友情链接
	public function addyqlj(){
		if(Db::name('cms_cooperation')->where('title',$_POST['title'])->find()){
			$this->error('链接名称已经存在添加失败','cooperation/index');
		}
		$data = [];
		$data = [
			'title'=>$_POST['title'],
			'url' =>$_POST['url'],
			'createtime'=>date('Y-m-d H:i:s'),
			'statue'=>$_POST['statue'],
			'type' => 1,
		];

		if(Db::name('cms_cooperation')->insert($data)){
			$this->success('添加成功！','cooperation/index');
		}else{
			$this->error('添加失败','cooperation/index');
		}
	}

	//修改友情链接状态
	public function statue(){
		$cms = Db::name('cms_cooperation')
				->where('id',$_GET['id'])
				->find();
		if($cms['status']==1){
			if(Db::name('cms_cooperation')->where(['id'=>$_GET['id']])->update(['status'=>0])){
                return "<script>window.location.href = document.referrer;</script>";
			}else{
                return "<script>window.location.href = document.referrer;</script>";
			}
		}else if($cms['status']==0){
			if(Db::name('cms_cooperation')->where(['id'=>$_GET['id']])->update(['status'=>1])){
                return "<script>window.location.href = document.referrer;</script>";
			}else{
                return "<script>window.location.href = document.referrer;</script>";
			}
		}
	}

	public function del(){
		$del = Db::name('cms_cooperation')->where(['id'=>$_POST['id']])->find();
		if($del){
			if(Db::name('cms_cooperation')->where(['id'=>$_POST['id']])->update(['status'=>2])){
				return 'success';
			}else{
				return 'error';
			}
		}
	}

	public function edityqlj(){
		if(empty($_POST)){
			$edit = Db::name('cms_cooperation')->where('id',$_GET['id'])->find();
			$this->assign('edit',$edit);
			return $this->fetch('cooperation/edityqlj');
		}else{
			$data = [
				'title' => $_POST['title'],
				'url'=>$_POST['url'],
				'statue'=>$_POST['statue'],
			];
			if(Db::name('cms_cooperation')->where('id',$_POST['id'])->update($data)){
				$this->success('修改成功','cooperation/index');
			}else{
				$this->error('修改失败','cooperation/index');
			}
		}
	}

	public function edithzhb(){
		if(empty($_POST)){
			$edit = Db::name('cms_cooperation')->where(['id'=>$_GET['id']])->find();
			$this->assign('edit',$edit);
			return $this->fetch('Cooperation/edithzhb');
		}else{
			$image = Db::name('cms_cooperation')->where('id',$_POST['id'])->field('photo')->find();
			$file = $this->request->file('img');			
			$data = [
				'title'=>$_POST['title'],
				'url'=>$_POST['url'],
				'statue'=>$_POST['statue'],
			];
			if($file){
				$info = $file->validate(['size'=>3300*1000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload/Cooperation');
	            if($info){
	            //成功上传后 获取上传信息
	            //输出 jpg
	                $info->getExtension();
	            //输出 42a79759f284b767dfcb2a0197904287.jpg
	                $img=$info->getFilename();
	                $data['photo'] = date('Ymd').'/'.$img;
	                unlink($_SERVER["DOCUMENT_ROOT"].'/upload/Cooperation/'.$image['photo']);
	         	}
			}else{
                $this->error('修改失败','cooperation/indexs');
            }
			if(Db::name('cms_cooperation')->where('id',$_POST['id'])->update($data)){
				$this->success('修改成功','cooperation/indexs');
			}else{
				$this->error('修改失败','cooperation/indexs');
			}
		}
	}


}